import { Component, EventEmitter, OnInit, Output } from "@angular/core";
import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzTreeNode } from "ng-zorro-antd/tree";
import { DeviceMaintainTaskService } from "../device-maintain-task.service";
import { NzUploadChangeParam, NzUploadFile } from "ng-zorro-antd/upload";
import { FileService } from "../../../sys/file.service";

@Component({
  selector: "app-device-maintain-task-edit",
  templateUrl: "./device-maintain-task-edit.component.html",
  styles: []
})
export class DeviceMaintainTaskEditComponent implements OnInit {


  constructor(private formBuilder: FormBuilder,
              private msgSrv: NzMessageService,
              private deviceMaintainTaskService: DeviceMaintainTaskService,
              public fileService: FileService
  ) {
  }

  nzLoading = false;

  @Output() voted = new EventEmitter();

  // 是否编辑
  isUpdate = false;

  // 只能查看
  isView = false;
  visible = false;
  // 保养单数据
  deviceMaintainTask: any = {};

  open(): void {
    this.visible = true;
    this.isUpdate = false;
  }

  edit(param) {
    this.visible = true;
    this.isView = false;
    this.deviceMaintainTask = param;
  }

  view(param){
    this.visible = true;
    this.isView = true;
    this.deviceMaintainTask = param;
  }

  close(): void {
    this.visible = false;
  }

  saveHandler() {
      const fileList = this.deviceMaintainTask.fileInfoList;
      fileList.forEach(file => {
          if (file.response) {
            file.id = file.response.data.id;
          }
        }
      );
  }

  submitForm(status): void {

    this.nzLoading = true;

    this.deviceMaintainTask.taskStatus = status;

    this.saveHandler();

    this.deviceMaintainTaskService.update(this.deviceMaintainTask).subscribe((res) => {

      this.nzLoading = false;

      if (res.code === 1) {
        this.msgSrv.success("保存完成");
        this.close();
        this.voted.emit();
      } else {
        this.msgSrv.error(res.message);
      }
    });
  }


  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

  ngOnInit(): void {

  }

  nzDisplayWith(node: NzTreeNode) {
    return node.title;
  }

  onChange($event: string): void {
    console.log($event);
  }

  handlePreview = async (file: NzUploadFile) => {

    let fileId = file.uid;

    if(file.response){
      fileId = file.response.data.id;
    }

    window.open("api/file/img/" + fileId)
  };

  handleDownload = async (file: NzUploadFile) => {

    this.fileService.download(file.uid);
  };

  handleChange(info: NzUploadChangeParam): void {

    if (info.file.status === "done") {
      if (info.file.response.code === 1) {
        this.msgSrv.success(`${info.file.name} file uploaded successfully`);
      } else {
        info.file.status = "error";
        this.msgSrv.error(`${info.file.name} file upload failed.${info.file.response.message}`);
      }

    } else if (info.file.status === "error") {
      this.msgSrv.error(`${info.file.name} file upload failed.`);
    }
  }
}
